package me.chan.completableFuture;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Future;
import java.util.Random;

import static me.chan.completableFuture.Util.format;
import static me.chan.completableFuture.Util.delay;

public class AsyncShop {

	 private final String name;
	 private final Random random;

	 public AsyncShop(String name) {
	        this.name = name;
	        this.random = new Random(name.charAt(0) * name.charAt(1) * name.charAt(2));
	 }
	
	public Future<Double> getPriceAsync(String product) {
		CompletableFuture<Double> future = new CompletableFuture<>();
		new Thread( () -> {
					double price = calculatePrice(product);
					future.complete(price);
				}
		).start();
		return future;
	}
	
	 private double calculatePrice(String product) {
	        delay();
	        return format(random.nextDouble() * product.charAt(0) + product.charAt(1));
	    }

}
